﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.ComponentModel;
using FrameWork.Interface;

//[assembly: WebResource("FrameWork.WebControls.Resource.bgAddSch.gif", "image/gif")]
namespace Slja.CC
{
    /// <summary>
    /// 文本框的可编辑性枚举
    /// </summary>
    public enum EditViewState
    {
        /// <summary>
        /// 用户自由编辑，显示为Input
        /// </summary>
        UserFreeEdit,
        /// <summary>
        /// 客户端JS才可编辑，显示为Input
        /// </summary>
        ClientJsEdit,
        /// <summary>
        /// Server后台可编辑，浏览器端编辑无效，显示为Input
        /// </summary>
        OnlyServerEdit,
        /// <summary>
        /// Server后台可编辑，显示为Label
        /// </summary>
        OnlyServerLabel
    }
    
    [ToolboxData("<{0}:TextBoxLabel runat=\"server\"></{0}:TextBoxLabel>")]
    public class TextBoxLabel:System.Web.UI.WebControls.TextBox
    {
        /// <summary>
        /// 编辑视图
        /// </summary>
        [Description("可编辑"), Category("属性"), DefaultValue(EditViewState.UserFreeEdit)]        
        public EditViewState EditView
        {
            get {
                
                if (ViewState[this.ID+"_EditView"] == null)
                {
                    IOperateView parent = ((object)this.Parent) as IOperateView;
                    if (parent != null)
                    {
                        if (parent.OpRight == FrameWork.Components.OpRightState.Editable
                            || parent.OpRight == FrameWork.Components.OpRightState.Deletable)
                        {                            
                            ViewState[this.ID + "_EditView"] = EditViewState.UserFreeEdit;
                        }
                        else if (parent.OpRight == FrameWork.Components.OpRightState.Readonly)
                        {
                            ViewState[this.ID + "_EditView"] = EditViewState.OnlyServerLabel;
                        }
                        else if (parent.OpRight == FrameWork.Components.OpRightState.Hidden)
                        {
                            ViewState[this.ID + "_EditView"] = EditViewState.OnlyServerLabel;//不可编辑
                        }
                    }
                    else
                    {
                        ViewState[this.ID + "_EditView"] = EditViewState.UserFreeEdit;
                    }
                }
                return (EditViewState)ViewState[this.ID+"_EditView"];
            }
            set { ViewState[this.ID+"_EditView"] = value; }
        }
        /// <summary>
        /// “添加”按钮，click事件函数
        /// </summary>
        public string BtnAddOfJsFunc
        {
            get
            {
                if (ViewState[this.ID+"_OpJsFunc"] == null)
                {
                    ViewState[this.ID+"_OpJsFunc"] = "";
                }
                return (string)ViewState[this.ID + "_OpJsFunc"];
            }
            set { ViewState[this.ID + "_OpJsFunc"] = value; }
        }
        /// <summary>
        /// 字段备注，描述内容
        /// </summary>
        public string Description
        {
            get
            {
                if (ViewState[this.ID + "_CusInputDescription"] == null)
                {
                    ViewState[this.ID + "_CusInputDescription"] = "";
                }
                return (string)ViewState[this.ID + "_CusInputDescription"];
            }
            set { ViewState[this.ID + "_CusInputDescription"] = value; }
        }
        /// <summary>
        /// 是否给必填提示
        /// </summary>
        public bool IsRequired
        {
            get
            {
                if (ViewState[this.ID + "_IsRequired"] == null)
                {
                    ViewState[this.ID + "_IsRequired"] = false;
                }
                return (bool)ViewState[this.ID + "_IsRequired"];
            }
            set { ViewState[this.ID + "_IsRequired"] = value; }
        }
        public override string CssClass
        {
            get
            {
                if (string.IsNullOrEmpty(base.CssClass))
                {
                    base.CssClass = "inputs";
                }
                return base.CssClass;
            }
            set
            {
                base.CssClass = value;
            }
        }
        protected override void Render(HtmlTextWriter writer)
        {
            if (CssClass.ToUpper() == "WDATE" &&
                (EditView == EditViewState.UserFreeEdit || EditView == EditViewState.ClientJsEdit))
            {
                base.Attributes["onfocus"] = "WdatePicker({skin:'whyGreen'})";
            }
            switch (EditView)
            {
                case EditViewState.UserFreeEdit:
                    base.Render(writer);
                    break;
                case EditViewState.ClientJsEdit:
                    base.Attributes["readonly"] = "readonly";
                    base.Render(writer);
                    break;
                case EditViewState.OnlyServerEdit:
                    base.ReadOnly = true;
                    base.Render(writer);
                    break;
                case EditViewState.OnlyServerLabel:
                    base.Visible = false;
                    writer.Write("<label id='"+base.ClientID+"'>"+this.Text+"</label>&nbsp;");
                    break;
            }
            if (!string.IsNullOrEmpty(BtnAddOfJsFunc) && EditView != EditViewState.OnlyServerLabel)
            {
                writer.Write("<img src='/image/proj/bgAddSch.gif' onclick='" + BtnAddOfJsFunc + "' style='position:relative;left:1px;top:5px;cursor:pointer;border:0;padding:0;margin:0;' />");
            }
            if (!string.IsNullOrEmpty(Description) && EditView != EditViewState.OnlyServerLabel
                && EditView != EditViewState.OnlyServerEdit)
            {
                writer.Write("(<span>"+Description+"</span>)");
            }
            if (IsRequired && (EditView == EditViewState.ClientJsEdit | EditView== EditViewState.UserFreeEdit))
            {
                writer.Write("<script type='text/javascript'>FieldRequired('"+this.ClientID+"');</script>");
            }
        }
        private string GetWebResourceUrl(string img)
        {
            return Page.ClientScript.GetWebResourceUrl(typeof(TextBoxLabel), "FrameWork.WebControls.Resource." + img);
        }
       
    }
}